<template>
    <section>
        <div style="margin-bottom:5px;">
            <el-input placeholder="请输入内容" size="small" v-model="searchKey" style="width: 250px;">
                <el-button slot="append" size="small" icon="el-icon-search" @click="getDataList"></el-button>
            </el-input>
            <el-button type="primary" size="small" @click="openAddDialog()">新 建</el-button>
        </div>
        <el-table ref="multipleTable" :data="dataList" border tooltip-effect="dark" style="width: 100%"
                  @selection-change="handleSelectionChange">
            <el-table-column type="selection" width="55" label="序号">
            </el-table-column>
            <el-table-column prop="code" label="编码">
            </el-table-column>
            <el-table-column prop="name" label="名称">
            </el-table-column>
            <el-table-column prop="status" label="状态">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.status " type="success">上架</el-tag>
                    <el-tag v-else type="danger">下架</el-tag>
                </template>
            </el-table-column>
            <el-table-column prop="requestUrl" label="请求Url">
            </el-table-column>

            <el-table-column prop="cache" label="是否缓存">
            </el-table-column>

            <el-table-column fixed="right" label="操作" width="200">
                <template slot-scope="scope">
                    <el-button-group>
                        <el-button size="mini" type="primary" @click="openDialog(scope.row)">编辑</el-button>
                        <el-button size="mini" type="danger" @click="removeItem(scope.row)">删除</el-button>
                    </el-button-group>
                </template>
            </el-table-column>
        </el-table>
        <div style="text-align: right;background: rgb(239, 242, 247);border: 1px solid #E5E9F2;">
            <el-pagination style="line-height: 30px; height: 30px;" @size-change="handleSizeChange"
                           @current-change="handleCurrentChange" :current-page="currentPage"
                           :page-sizes="[10, 20, 30, 50]" :page-size="pageSize"
                           layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </div>
        <div v-if="dialogFormVisibleAdd">
            <el-dialog :title="dialogAddOrEdit" :visible="dialogFormVisibleAdd" :modal="dialogFormVisibleAdd"
                       :before-close="openAddDialogClose">

                <el-form label-width="100px" :rules="formAddRules" :model="formAdd" ref="formAdd">
                    <el-collapse v-model="activeNames" style=" padding-left: 20px;padding-right: 20px">
                        <el-collapse-item title="基本信息" name="1">
                            <el-row>
                                <el-col :span=12>
                                    <el-form-item label="编码" prop="code">
                                        <el-input v-model="formAdd.code" style="width: 100%" :disabled="formAdd.id !=null"></el-input>
                                        <div >新增后不允许修改</div>
                                    </el-form-item>
                                </el-col>

                                <el-col :span=12>
                                    <el-form-item label="名称" prop="name">
                                        <el-input v-model="formAdd.name" style="width: 100%"></el-input>
                                    </el-form-item>
                                </el-col>

                            </el-row>

                            <el-row>
                                <el-col :span=12>
                                    <el-form-item label="状态" prop="status">
                                        <el-switch   on-text="上架" off-text="下架" v-model="formAdd.status"></el-switch>
                                    </el-form-item>
                                </el-col>

                                <el-col :span=12>

                                </el-col>

                            </el-row>
                        </el-collapse-item>
                        <el-collapse-item title="缓存设置" name="2">
                            <el-row>
                                <el-col :span=12>
                                    <el-form-item label="缓存" prop="cache">
                                        <el-switch on-text="是" off-text="否" v-model="formAdd.cache"></el-switch>

                                    </el-form-item>
                                </el-col>

                                <el-col :span=12>
                                    <el-form-item label="缓存时间(s)" prop="cacheTime">
                                        <el-input v-model="formAdd.cacheTime" style="width: 100%"></el-input>
                                    </el-form-item>
                                </el-col>

                            </el-row>
                            <el-row>
                                <el-col :span=12>
                                    <el-form-item label="缓存Key" prop="cacheKey">
                                        <el-input v-model="formAdd.cacheKey" style="width: 100%"></el-input>
                                    </el-form-item>
                                </el-col>

                                <el-col :span=12>

                                </el-col>

                            </el-row>
                        </el-collapse-item>
                        <el-collapse-item title="请求设置" name="3">

                            <el-row>
                                <el-col :span=12>
                                    <el-form-item label="请求方法" prop="requestMethod">
                                        <el-select v-model="formAdd.requestMethod" placeholder="请选择">
                                            <el-option
                                                    key="GET"
                                                    label="GET"
                                                    value="GET">
                                            </el-option>
                                            <el-option
                                                    key="POST"
                                                    label="POST"
                                                    value="POST">
                                            </el-option>
                                            <el-option
                                                    key="POSTBODY"
                                                    label="POSTBODY"
                                                    value="POSTBODY">
                                            </el-option>
                                            <el-option
                                                    key="DELETE"
                                                    label="DELETE"
                                                    value="DELETE">
                                            </el-option>
                                            <el-option
                                                    key="PUT"
                                                    label="PUT"
                                                    value="PUT">
                                            </el-option>
                                        </el-select>

                                    </el-form-item>
                                </el-col>

                                <el-col :span=12>
                                    <el-form-item label="请求地址" prop="requestUrl">
                                        <el-input v-model="formAdd.requestUrl" style="width: 100%"></el-input>
                                    </el-form-item>
                                </el-col>

                            </el-row>
                            <el-row>
                                <el-col :span=24>
                                    <el-table
                                            :data="formAdd.requestFieldVoList"
                                            border
                                            style="width: 100%">
                                        <el-table-column
                                                label="字段编码">
                                            <template slot-scope="scope">
                                                <el-input v-model="scope.row.fieldCode"
                                                          placeholder="请输入字段编码"></el-input>
                                            </template>
                                        </el-table-column>
                                        <el-table-column
                                                label="字段名称"
                                                　>
                                            <template slot-scope="scope">
                                                <el-input v-model="scope.row.fieldName" placeholder="请输入字段名称"
                                                ></el-input>
                                            </template>
                                        </el-table-column>
                                        <el-table-column
                                                label="字段类型"
                                                　>
                                            <template slot-scope="scope">

                                                <el-select v-model="scope.row.fieldType" placeholder="请选择字段类型">
                                                    <el-option
                                                            key="String"
                                                            label="String"
                                                            value="String">
                                                    </el-option>
                                                    <el-option
                                                            key="Number"
                                                            label="Number"
                                                            value="Number">
                                                    </el-option>
                                                    <el-option
                                                            key="Date"
                                                            label="Date"
                                                            value="Date">
                                                    </el-option>
                                                    <el-option
                                                            key="DateTime"
                                                            label="DateTime"
                                                            value="DateTime">
                                                    </el-option>
                                                    <el-option
                                                            key="Double"
                                                            label="Double"
                                                            value="Double">
                                                    </el-option>
                                                </el-select>
                                            </template>
                                        </el-table-column>

                                        <el-table-column
                                                label="默认值" 　>
                                            <template slot-scope="scope">
                                                <el-input v-model="scope.row.defaultValue" placeholder="请输入默认值"
                                                ></el-input>
                                            </template>
                                        </el-table-column>
                                        <el-table-column
                                                label="必填">
                                            <template slot-scope="scope">
                                                <el-switch on-text="是" off-text="否"
                                                           v-model="scope.row.required"></el-switch>
                                            </template>
                                        </el-table-column>
                                        <el-table-column label="操作" 　:render-header="renderHeader" 　>
                                            <template slot-scope="scope">
                                                <el-button-group>
                                                    <el-button size="mini" type="danger"
                                                               @click="renderRemoveRow(scope.$index)">
                                                        删除
                                                    </el-button>
                                                </el-button-group>
                                            </template>
                                        </el-table-column>
                                    </el-table>
                                </el-col>
                            </el-row>
                            　
                        </el-collapse-item>
                    </el-collapse>

                    <el-form-item>
                        <el-button type="primary" @click="submitFormAdd('formAdd')">保存</el-button>
                        <el-button @click="openAddDialogClose">取消</el-button>
                    </el-form-item>
                </el-form>
            </el-dialog>
        </div>
    </section>
</template>
<style>

</style>
<script>
    import {AjaxByAll} from '@/api/api';

    export default {
        data() {

            return {
                activeNames: ['1'],
                dataList: [],
                multipleSelection: [],
                dialogFormVisibleAdd: false,
                labelPosition: 'left',
                projectCode: null,
                formAdd: {
                    id: null,
                    code: null,
                    name: null,
                    status: null,
                    cache: null,
                    cacheTime: null,
                    cacheKey: null,
                    requestMethod: null,
                    requestUrl: null,
                    requestFieldVoList: [],
                    projectCode: null,
                },

                pageSize: 10,
                currentPage: 1,
                total: 0,
                dialogAddOrEdit: '',
                searchKey: null, defaultData: null,
                formAddRules: {
                    code: [
                        {required: true, message: '请输入 接口编码', trigger: 'blur'}
                    ],
                    name: [
                        {required: true, message: '请输入 接口名称', trigger: 'blur'}
                    ],
                }
            }
        },

        methods: {
            renderHeader(h, params) {
                let a = [h('el-button', {
                    props: {
                        size: "mini",
                        type: "primary"
                    },
                    on: {
                        click: () => {
                            this.renderAddRow();
                        }
                    }
                }, '增加')];
                return h('div', a);
            },

            renderAddRow() {
                this.formAdd.requestFieldVoList.push({
                    "fieldCode": "",
                    "fieldName": "",
                    "fieldType": "String",
                    "defaultValue": "",
                    "required": false
                });
            },
            renderRemoveRow(index) {

                this.formAdd.requestFieldVoList.splice(index, 1);
            },
            openDialog(model) {

                if (model) {
                    let id = model.id;
                    this.dialogAddOrEdit = '编辑接口';
                    let urls = 'pt/interface/' + id;
                    let method = 'get';
                    let form = {};
                    AjaxByAll(method, urls, form).then(data => {
                        if (data.code === 200) {
                            this.formAdd = data.data;
                        } else {
                            this.$message.error(data.msg);
                        }
                    });
                }
                this.dialogFormVisibleAdd = true;
            },
            openAddDialog() {
                this.dialogAddOrEdit = '新建接口';
                // 送出後
                this.$data.formAdd = Object.assign(this.$data.formAdd, this.defaultData);
                this.formAdd.projectCode = this.projectCode;
                this.dialogFormVisibleAdd = true;
            },
            openAddDialogClose() {

                this.dialogFormVisibleAdd = false;
            },
            getDataList() {
                let self = this;
                let form = {
                    currentPage: self.currentPage,
                    pageSize: self.pageSize,
                    keyword: self.searchKey,
                    projectCode: self.projectCode,
                };
                let urls = 'pt/interface/query';
                AjaxByAll('post', urls, form).then(data => {
                    if (data.code === 200) {

                        self.total = data.data.total * 1;
                        self.dataList = data.data.records;
                    }

                    this.loading = false;
                });
            },
            submitFormAdd(formName) {

                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        let form = this.formAdd;
                        let urls = 'pt/interface/saveOrUpdateInterface';
                        let method = 'post';
                        AjaxByAll(method, urls, form).then(data => {
                            if (data.code === 200) {
                                this.formAdd.id = data.data;
                                this.$message({
                                    message: '操作成功',
                                    type: 'success'
                                });

                                this.getDataList();
                            } else {
                                this.$message({
                                    message: data.msg,
                                    type: 'error'
                                });
                            }
                            this.dialogFormVisibleAdd = !this.dialogFormVisibleAdd;
                        });


                    } else {
                        this.$message({
                            message: '校验失败',
                            type: 'error'
                        });
                    }
                });
            },
            removeItem(model) {
                this.$confirm('确认移除吗?', '提示', {
                    type: 'warning'
                }).then(() => {
                    AjaxByAll('delete', 'pt/interface/' + model.id, null).then(data => {
                        if (data.code === 200) {
                            this.$message({
                                message: '操作成功',
                                type: 'success'
                            });
                            this.getDataList();
                        } else {
                            this.$message({
                                message: '操作失败：' + data.msg,
                                type: 'error'
                            });
                        }

                    });
                }).catch(() => {

                });
            },
            toggleSelection(rows) {
                if (rows) {
                    rows.forEach(row => {
                        this.$refs.multipleTable.toggleRowSelection(row);
                    });
                } else {
                    this.$refs.multipleTable.clearSelection();
                }
            },
            handleSelectionChange(val) {
                this.multipleSelection = val;
            },
            handleSizeChange(val) {
                //  console.log(`每页 ${val} 条`);
                this.pageSize = val;
                this.getDataList();
            },
            handleCurrentChange(val) {
                // console.log(`当前页: ${val}`);
                this.currentPage = val;
                this.getDataList();
            }
        },
        created() {

        },
        mounted: function () {

            this.projectCode = this.$route.params.projectCode;

            this.getDataList();
            this.defaultData = JSON.parse(JSON.stringify(this.formAdd));
        }
    }
</script>
